/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ELE.EnterpriseLibrary.Data.Configuration;

namespace ELE.EnterpriseLibrary.Data
{
  /// <summary>
  /// Custom factory for the object storage from configuration settings.
  /// </summary>
  public class ObjectStorageManagerCustomFactory : ICustomFactory
  {
    /// <summary>
    /// Creates an instance of <see cref="ObjectStorageManager"/> from configuration source.
    /// </summary>
    /// <param name="context"></param>
    /// <param name="name"></param>
    /// <param name="configurationSource"></param>
    /// <param name="reflectionCache"></param>
    /// <returns></returns>
    public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
    {
      ObjectStorageConfigurationView view = new ObjectStorageConfigurationView(configurationSource);
      ObjectStorageManager createdObject = null;
      ObjectStorageData storageData = view.GetStorageData(name);
      if (storageData != null)
      {
        ObjectStorage store = new ObjectStorage(name);
        IConfigurationSource fileSource = new FileConfigurationSource(storageData.FilePath);

        ObjectSqlMappingConfigurationView mappingView = new ObjectSqlMappingConfigurationView(fileSource);

        store.DefaultMappingName = mappingView.ObjectMappingSettings.DefaultMapping;
        store.DefaultQueryName = mappingView.ObjectMappingSettings.DefaultQuery;

        ObjectSqlMappingManagerCustomFactory mappingFactory = new ObjectSqlMappingManagerCustomFactory();


        foreach (MappingQueryData mapData in mappingView.ObjectMappingSettings.Mappings)
        {
          MappingQueryData queryData = mappingView.GetMappingQueryData(mapData.Name);
          if (queryData != null)
          {
            foreach (ObjectMappingData oMap in queryData.Objects)
            {
              ObjectSqlMapping map = mappingFactory.GetMappingQuery(mappingView, queryData.Name, oMap.Name);
              map.MapName = queryData.Name;
              if (map != null)
              {
                store.AddMapping(map);
              }
            }

          }
        }
        ObjectSqlQueryManagerCustomFactory queryFactory = new ObjectSqlQueryManagerCustomFactory();
        foreach (SqlQueryData queryData in mappingView.ObjectMappingSettings.Queries)
        {
          ObjectSqlQueryManager man = (ObjectSqlQueryManager)queryFactory.CreateObject(context, queryData.Name, fileSource, reflectionCache);
          store.AddQuery(man.Query);
        }
        ObjectStoredProcedureManagerCustomFactory spFactory = new ObjectStoredProcedureManagerCustomFactory();
        foreach (StoredProcedureData spData in mappingView.ObjectMappingSettings.StoredProcedures)
        {
          ObjectStoredProcedureManager man = (ObjectStoredProcedureManager)spFactory.CreateObject(context, spData.Name, fileSource, reflectionCache);
          store.AddStoredProcedure(man.StoredProcedure);
        }

        createdObject = new ObjectStorageManager( store);
      }

      RegisterObject(context, name, createdObject);
      return createdObject;
    }
    private void RegisterObject(IBuilderContext context, string name, ObjectStorageManager createdObject)
    {
      if (context.Locator != null)
      {
        ILifetimeContainer lifetime = context.Locator.Get<ILifetimeContainer>(typeof(ILifetimeContainer), SearchMode.Local);

        if (lifetime != null)
        {
          context.Locator.Add(new DependencyResolutionLocatorKey(typeof(ObjectStorageManager), name), createdObject);
          lifetime.Add(createdObject);
        }
      }
    }
  }

}
